package com.lansi.card.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lansi.bean.ResponseResult;
import com.lansi.card.bean.Card;
import com.lansi.card.service.ICardService;

@Controller
public class CardController {

	@Resource
	private ICardService cardService;
	
	@RequestMapping("/getInsertCard.do")
	@ResponseBody
	public ResponseResult<Void> getInsertCard(@RequestBody Map<String,String> map1) 
			throws JsonParseException, JsonMappingException, IOException, ParseException{
		
		ResponseResult<Void> rr = new ResponseResult<Void>();
		
		ObjectMapper mapper = new ObjectMapper();    
		@SuppressWarnings("unchecked")
		Map<String,Object> map = mapper.readValue(map1.get("params"), Map.class); 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Card card = new Card();
		
		try {
			String cardid = map.get("cardid").toString();
			String jor = "";
			String nor = "";
			for (int i = 0; i < cardid.length(); i++) {
			    int ch = (int) cardid.charAt(i);
			    String s4 = Integer.toHexString(ch);
			    if("a".equals(s4)) {
			    	s4 = "10";
			    }
			    if("b".equals(s4)) {
			    	s4 = "11";
			    }
			    if("c".equals(s4)) {
			    	s4 = "12";
			    }
			    if("d".equals(s4)) {
			    	s4 = "13";
			    }
			    if("e".equals(s4)) {
			    	s4 = "14";
			    }
			    if("f".equals(s4)) {
			    	s4 = "15";
			    }
			    nor = nor + s4;
			}
			for (int i = 1; i < cardid.length(); i++) {
			    int ch = (int) cardid.charAt(i);
			    String s4 = Integer.toHexString(ch);
			    if("a".equals(s4)) {
			    	s4 = "10";
			    }
			    if("b".equals(s4)) {
			    	s4 = "11";
			    }
			    if("c".equals(s4)) {
			    	s4 = "12";
			    }
			    if("d".equals(s4)) {
			    	s4 = "13";
			    }
			    if("e".equals(s4)) {
			    	s4 = "14";
			    }
			    if("f".equals(s4)) {
			    	s4 = "15";
			    }
			    jor = jor + s4;
			}
			Object sn = map.get("sn");
			Integer major = Integer.parseInt(jor);
			Integer minor = Integer.parseInt(nor);
			Object inteval = map.get("inteval");
			Object power = map.get("power");
			Object batterycap = map.get("batterycap");
			Object usetime = map.get("usetime");
			Object memo = map.get("memo");
			
			if(cardid != null) card.setCardid(cardid.toString());
			if(sn != null) card.setSn(sn.toString());
			card.setMajor(major);
			card.setMinor(minor);
			if(inteval == null || "".equals(inteval.toString().trim())) {
				card.setInteval((Integer)1);
			}else {
				card.setInteval(Integer.parseInt(inteval.toString()));
			}
			if(power != null) card.setPower(power.toString());
			if(batterycap == null || "".equals(batterycap.toString().trim())) {
				card.setBatterycap((Integer)1);
			}else {
				card.setBatterycap(Integer.parseInt(batterycap.toString()));
			}
			if(usetime != null && !"".equals(usetime.toString().trim())) {
				card.setUsetime(sdf.parse(usetime.toString()));
			}
			if(memo != null) card.setMemo(memo.toString());
			cardService.getInsertCard(card);
			
			rr.setState(1);
			rr.setMessage("添加信息成功！");
			
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage("添加信息失败！");
			System.out.println(e.getMessage());
		}
	    
		return rr;
	}
	
	//删除信息
	@RequestMapping("/delCardByCardid.do")
	@ResponseBody
	public ResponseResult<Void> delCardByCardid(@RequestBody Map<String,String> map){
		ResponseResult<Void> rr = new ResponseResult<Void>();
		
		String cardid = map.get("cardid");
		try {
			cardService.delCardByCardid(cardid);
			rr.setState(1);
			rr.setMessage("信息删除成功！");
		} catch (Exception e) {
			rr.setState(-1);
			rr.setMessage("信息删除失败！");
			System.out.println(e.getMessage());
		}
		
		return rr;
	}
	
	//修改信息
	@RequestMapping("/getUpdateCardByCardid.do")
	@ResponseBody
	public ResponseResult<Void>	getUpdateCardByCardid(@RequestBody Map<String,String> map1) 
			throws JsonParseException, JsonMappingException, IOException, ParseException{
		
		ResponseResult<Void> rr = new ResponseResult<Void>();
		
		ObjectMapper mapper = new ObjectMapper();    
		@SuppressWarnings("unchecked")
		Map<String,Object> map = mapper.readValue(map1.get("params"), Map.class); 
		
		try {
			Object cardid = map.get("cardid");
			Object sn = map.get("sn");
			Object inteval = map.get("inteval");
			Object power = map.get("power");
			Object batterycap = map.get("batterycap");
			Object usetime = map.get("usetime");
			Object memo = map.get("memo");
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date utime = sdf.parse(usetime.toString());
			
			Card card = new Card();
			if(cardid != null) card.setCardid(cardid.toString());
			if(sn != null) card.setSn(sn.toString());
			if(inteval == null || "".equals(inteval.toString().trim())) {
				card.setInteval((Integer)1);
			}else {
				card.setInteval(Integer.parseInt(inteval.toString()));
			}
			if(power != null) card.setPower(power.toString());
			if(batterycap == null || "".equals(batterycap.toString().trim())) {
				card.setBatterycap((Integer)1000);
			}else {
				card.setBatterycap(Integer.parseInt(batterycap.toString()));
			}
			if(usetime != null && !"".equals(usetime.toString().trim())) {
				card.setUsetime(utime);
			}
			if(memo != null) card.setMemo(memo.toString());
			cardService.getUpdateCardByCardid(card);
			rr.setState(1);
			rr.setMessage("信息修改成功！");
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage("信息修改失败！");
			System.out.println(e.getMessage());
		}
		return rr;
	}
	
		//根据username进行分权限查询蓝牙工卡信息
		@RequestMapping("/getCardByUsername.do")
		@ResponseBody
		public ResponseResult<Map<String,Object>>  getCardByUsername(String username,String page	//,HttpServletRequest request
				){
			
//			User user = (User)request.getAttribute("user");
//			String prio = user.getPrio();
//			String sn = user.getSn();
			String sn = "2111";
			String prio = "2";//-------------------------------------------------------------------------------------------------------------
			Integer offset = null;
			Integer count = 10;
			if(page == null) {
				offset = 0;
			}else {
				offset = (Integer.parseInt(page)-1)*10;
			}
			Integer size = null;
			ResponseResult<Map<String, Object>> rr = new ResponseResult<Map<String, Object>>();
			Map<String, Object> map = new HashMap<String, Object>();
			
			try {
				if("1".equals(prio)) {
					List<Card> list = cardService.getCardAll(offset, count);
					size = cardService.getSizeCardAll();
					map.put("list", list);
					map.put("size", size);
					rr.setState(1);
					rr.setMessage("获取所有蓝牙工卡信息成功！");
					rr.setData(map);
				}else {
					List<Card> list = cardService.getCardBySn(sn, offset, count);
					size = cardService.getSizeCardBySn(sn);
					map.put("list", list);
					map.put("size", size);
					rr.setState(1);
					rr.setMessage("获取蓝牙工卡信息成功！");
					rr.setData(map);
					
				}
			} catch (RuntimeException e) {
				rr.setState(-1);
				rr.setMessage("获取蓝牙工卡信息失败！");
				System.out.println(e.getMessage());
			}
			
			return rr;
			
		}
		
		
		//根据username和cardid进行查询蓝牙工卡信息
		@RequestMapping("/getCardByUCardid.do")
		@ResponseBody
		public ResponseResult<Map<String,Object>>  getCardByUCardid(String username,String cardid//,HttpServletRequest request
				){
			
//			User user = (User)request.getAttribute("user");
//			String prio = user.getPrio();
//			String sn = user.getSn();
			String sn = "2111";
			String prio = "2";//-------------------------------------------------------------------------------------------------------------
			Integer count = 10;
			ResponseResult<Map<String, Object>> rr = new ResponseResult<Map<String, Object>>();
			Map<String, Object> map = new HashMap<String, Object>();
			List<Card> listutil = new ArrayList<Card>();
			try {
				if("1".equals(prio)) {
					Card card = cardService.getCardByCardid(cardid);
					if(card == null) {
						rr.setState(-1);
						rr.setMessage("该工卡号不存在！");
					}else {
						
						listutil.add(card);
						map.put("list", listutil);
						map.put("size", 1);
						rr.setState(1);
						rr.setMessage("获取信息成功！");
						rr.setData(map);
					}
				}else {
					List<Card> list = cardService.getCardBySn(sn, null, count);
					Card card =cardService.getCardByCardid(cardid);
					if(!list.contains(card) && card != null) {
						rr.setState(-1);
						rr.setMessage("该设备信息您无权限查看！");
					}else if(card == null) {
						rr.setState(-1);
						rr.setMessage("该工卡号不存在！");
					}else {
						
						listutil.add(card);
						map.put("list", listutil);
						map.put("size", 1);
						rr.setState(1);
						rr.setMessage("获取信息成功！");
						rr.setData(map);
					}
				}
			} catch (RuntimeException e) {
				rr.setState(-1);
				rr.setMessage("查询信息失败!");
				System.out.println(e.getMessage());
			}
			
			return rr;
		}
	
	
}
